package com.test.nnooolwdeaer.adapter_wooden;

import static android.view.View.GONE;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.nnooolwdeaer.adapter_wooden.base_wooden.BaseListAdapter_wooden;
import com.test.nnooolwdeaer.bean_wooden.config_info_wooden.AppShowInfo_wooden;
import com.test.nnooolwdeaer.bean_wooden.config_info_wooden.CustomerService_wooden;
import com.test.nnooolwdeaer.R;

import java.util.ArrayList;


public class CustomerServiceListAdapter_wooden extends BaseListAdapter_wooden {

    private final Context context_wooden;

    private static String hotlineTitle_wooden = "";
    private static String emailTitle_wooden = "";

    private final ArrayList<CustomerService_wooden> customerServices_wooden = new ArrayList<>();

    public CustomerServiceListAdapter_wooden(Context context){
        this.context_wooden = context;
        hotlineTitle_wooden = context.getString(R.string.customer_service_hotline);
        emailTitle_wooden = context.getString(R.string.customer_service_email);
        initCustomerService_wooden();
    }

    @Override
    public int getCount() {
        return customerServices_wooden.size();
    }

    @Override
    public CustomerService_wooden getItem(int position) {
        return customerServices_wooden.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void clear_wooden(){
        customerServices_wooden.clear();
    }

    public void add_wooden(CustomerService_wooden customerService){
        customerServices_wooden.add(customerService);
    }

    @SuppressLint({"SetTextI18n", "InflateParams"})
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view==null) view = LayoutInflater.from(context_wooden).inflate(R.layout.item_customer_service_wooden,parent,false);

        TextView titleTv = view.findViewById(R.id.item_customer_service_tv_title);
        TextView textTv = view.findViewById(R.id.item_customer_service_tv_text);
        ViewGroup btnLayout = view.findViewById(R.id.item_customer_service_layout_btn);
        ImageView btnIv  = view.findViewById(R.id.item_customer_service_iv_btn);
        TextView btnTv = view.findViewById(R.id.item_customer_service_tv_btn);

        CustomerService_wooden customerService = customerServices_wooden.get(position);

        titleTv.setText(customerService.getServiceTitle_wooden());
        String text = customerService.getServiceText_wooden();
        textTv.setText(text);

        if (customerService.getServiceType_wooden() == CustomerService_wooden.CustomerServiceType_wooden.Hotline){
            btnIv.setImageResource(R.drawable.icon_dial);
            btnTv.setText(context_wooden.getString(R.string.word_dial));
            btnLayout.setOnClickListener(v -> {
                String mobile =customerService.getServiceText_wooden().trim();
                String[] mobileArr = mobile.split(" ");
                if (mobileArr.length>0) mobile = mobileArr[mobileArr.length-1];
                Intent dialIntent_juucash =  new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mobile));//跳转到拨号界面，同时传递电话号码
                context_wooden.startActivity(dialIntent_juucash);
            });
        }else if (customerService.getServiceType_wooden() == CustomerService_wooden.CustomerServiceType_wooden.Email){
            btnIv.setImageResource(R.drawable.icon_copy_blue);
            btnTv.setText(context_wooden.getString(R.string.word_copy));
            btnLayout.setVisibility(GONE);
        }

        return view;
    }

    public void initCustomerService_wooden(){
        clear_wooden();
        //添加服务热线
        String[] serviceHotlines = AppShowInfo_wooden.getInstance().getMobile_wooden();
        for (String serviceHotline:serviceHotlines){
            if (!serviceHotline.isEmpty()){
                add_wooden(new CustomerService_wooden(
                        CustomerService_wooden.CustomerServiceType_wooden.Hotline,
                        hotlineTitle_wooden,
                        serviceHotline
                ));
            }
        }

        //添加服务邮箱
        String[] serviceEmails = AppShowInfo_wooden.getInstance().getEmail_wooden();
        for (String serviceEmail:serviceEmails){
            if (!serviceEmail.isEmpty()){
                add_wooden(new CustomerService_wooden(
                        CustomerService_wooden.CustomerServiceType_wooden.Email,
                        emailTitle_wooden,
                        serviceEmail
                ));
            }
        }
    }
}
